package com.xiang._03.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

public class NioClientSocket {
    public static void main(String[] args) throws IOException {
        //1、打开通道
        SocketChannel socketChannel = SocketChannel.open();
        //2、连接服务端 ip+port
        socketChannel.connect(new InetSocketAddress("localhost",9999));
        //3、写数据
        socketChannel.write(ByteBuffer.wrap("来着客户端消息".getBytes(StandardCharsets.UTF_8)));
        //4、读取来着服务端数据
        ByteBuffer byteBuffer = ByteBuffer.allocate(50);
        int read = socketChannel.read(byteBuffer);
        System.out.println("读到服务端消息："+new String(byteBuffer.array(),0,read,StandardCharsets.UTF_8));
        //5、释放资源
        socketChannel.close();

    }
}
